﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing.Design;

namespace UtfprLibrary.Windows.Forms {
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(ResourceFinder), "Resources.VSD_UtfprByKey.png")]
    public partial class UtfprByKey : UtfprByKeyBase {
        #region Base

        [Browsable(false)]
        public override TextBox TextBoxId {
            get { return this.txtId; }
            set { this.txtId = value; }
        }

        private bool _isString = false;
        [Category("Behavior")]
        public override bool IsString {
            get { return _isString; }
            set { _isString = value; }
        }

        private bool _isCaseInsensitive = false;
        [Category("Behavior")]
        public override bool IsCaseInsensitive {
            get { return _isCaseInsensitive; }
            set { _isCaseInsensitive = value; }
        }

        [Category("Layout")]
        public override int TextBoxWidth {
            get { return txtId.Width; }
            set { txtId.Width = value; }
        }

        [Browsable(false)]
        public override UtfprButtonWithMouseOver ButtonGo {
            get { return this.btnGo; }
            set { this.btnGo = value; }
        }

        #endregion

        #region Constructor

        public UtfprByKey() {
            InitializeComponent();
            txtId.KeyPress += new KeyPressEventHandler(txtId_KeyPress);
        }

        #endregion

        #region EventHandlers

        void txtId_KeyPress(object sender, KeyPressEventArgs e) {
            //habilita o backspace e o delete
            if (e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete) {
                e.Handled = false;
                return;
            }

            if (!IsString) {
                //bloqueia os caracteres não numéricos
                if (!Regex.IsMatch(e.KeyChar.ToString(), "\\d+")) {
                    e.Handled = true;
                }
            }
        }

        #endregion
    }
}
